﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.ComponentModel;

namespace WpfDrawing
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private Model3D _model;
        /// <summary>
        /// Gets or sets the model.
        /// </summary>
        /// <value>The model.</value>
        public Model3D Model 
        { 
            get { return _model; }
            set { _model = value; 
                this.PropertyChanged(this, new PropertyChangedEventArgs("Model")); } 
        }

        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = this;
        }

        private IList<Point> points = new List<Point>();

        private Polyline line = null;
        private Polygon poly = null;
        private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point point = Mouse.GetPosition(this);
                points.Add(point);

                if (line == null)
                {
                    line = new Polyline();
                    line.Stroke = Brushes.White;
                    line.StrokeThickness = 2;
                    Canvas.Children.Add(line);
                }
                line.Points.Add(point);
            }
            if (e.RightButton == MouseButtonState.Pressed)
            {
                Canvas.Children.Remove(line);
                line = null;

                poly = new Polygon();
                poly.Points = new  PointCollection(points);
                poly.Stroke = Brushes.Black;
                poly.StrokeThickness = 2;
                poly.Fill = Brushes.Yellow;

                points = new List<Point>();
                Canvas.Children.Add(poly);
            }

        }

        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            int height = 100;
            Model3DGroup group = new Model3DGroup();

            GeometryModel3D geometryCanvas = new GeometryModel3D();
            GeometryModel3D geometry = new GeometryModel3D();

            MeshGeometry3D mesh = new MeshGeometry3D();
            MeshGeometry3D meshCanvas = new MeshGeometry3D();

            // nastaveni geometrie
            geometry.Geometry = mesh;
            geometryCanvas.Geometry = meshCanvas;

            #region  Visual brush

            // 
            VisualBrush myVisualBrush = new VisualBrush();

            // Create the visual brush's contents.
            StackPanel myStackPanel = new StackPanel();
            myStackPanel.Background = Brushes.White;

            Rectangle redRectangle = new Rectangle();
            redRectangle.Width = 25;
            redRectangle.Height = 25;
            redRectangle.Fill = Brushes.Red;
            redRectangle.Margin = new Thickness(2);
            myStackPanel.Children.Add(redRectangle);

            TextBlock someText = new TextBlock();
            FontSizeConverter myFontSizeConverter = new FontSizeConverter();
            someText.FontSize = (double)myFontSizeConverter.ConvertFrom("10pt");
            someText.Text = "Hello, World!";
            someText.Margin = new Thickness(2);
            myStackPanel.Children.Add(someText);

            Button aButton = new Button();
            aButton.Content = "A Button";
            aButton.Margin = new Thickness(2);
            myStackPanel.Children.Add(aButton);

            // Use myStackPanel as myVisualBrush's content.
            myVisualBrush.Visual = myStackPanel;

            #endregion

            // Ink Canvas
            myVisualBrush.Visual = InkCanvas;

            // nastaveni barvy
            geometry.Material = new DiffuseMaterial(Brushes.Aqua);
            geometry.BackMaterial = new DiffuseMaterial(Brushes.Yellow);

            geometryCanvas.Material = new DiffuseMaterial(myVisualBrush); // new DiffuseMaterial(Brushes.Gray);
            geometryCanvas.BackMaterial = new DiffuseMaterial(Brushes.Purple);

            // body
            int i = 0;
            foreach (Point point in poly.Points)
            {
                mesh.Positions.Add(new Point3D(point.X, point.Y, 0));
                mesh.Positions.Add(new Point3D(point.X, point.Y, height));

                if (i <= 2)
                {
                    meshCanvas.Positions.Add(new Point3D(point.X, point.Y, 0));
                    meshCanvas.Positions.Add(new Point3D(point.X, point.Y, height));
                }

                i += 2;
            }

            bool isFirst = true;

            // trojuhelniky
            for (int j = 0; j < i; j++)
            {
                MeshGeometry3D msh = isFirst ? meshCanvas : mesh;
                if (isFirst) isFirst = false;

                if (j % 2 == 0)
                {
                    msh.TriangleIndices.Add(j % i);
                    msh.TriangleIndices.Add((j + 1) % i);
                    msh.TriangleIndices.Add((j + 3) % i);

                    msh.TriangleIndices.Add(j % i);
                    msh.TriangleIndices.Add((j + 3) % i);
                    msh.TriangleIndices.Add((j + 2) % i);
                }

                if (j % 2 == 1)
                {
                    msh.TriangleIndices.Add(j % i);
                    msh.TriangleIndices.Add((j + 2) % i);
                    msh.TriangleIndices.Add((j + 4) % i);
                }
            }

            // textur koordinaty 0,1 1,1 1,0 0,0  - 1,1 0,1 0,0 1,0
            
            mesh.TextureCoordinates.Add(new Point(1, 1));
            mesh.TextureCoordinates.Add(new Point(1, 0));
            mesh.TextureCoordinates.Add(new Point(0, 1));
            mesh.TextureCoordinates.Add(new Point(0, 0));

            // 
            meshCanvas.TextureCoordinates.Add(new Point(1, 1));
            meshCanvas.TextureCoordinates.Add(new Point(1, 0));
            meshCanvas.TextureCoordinates.Add(new Point(0, 1));
            meshCanvas.TextureCoordinates.Add(new Point(0, 0));

            group.Children.Add(geometryCanvas);
            group.Children.Add(geometry);
            Model = group;

            ThreeDim.Visibility = System.Windows.Visibility.Visible;
            Canvas.Visibility = Visibility.Hidden;
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };
    }
}
